/**
 *
 * Copyright by Samuel Castro & Joaquim Fonseca, 2011
 * All Rights Reserved
 *
 * NOTES:
 * Please do not redistribute this plugin without my permission
 * Plugin made for private use only
 *
 */
package com.SySammy.sdMiningChallenge;

import com.SySammy.sdMiningChallenge.Listeners.sdPlayerListener;
import com.SySammy.sdMiningChallenge.Listeners.sdMinningListener;
import com.SySammy.sdMiningChallenge.prop.MiningProperties;
import com.nijiko.permissions.PermissionHandler;
import com.nijikokun.bukkit.Permissions.Permissions;
import java.io.File;
import java.util.logging.Logger;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.Event.Priority;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;

public class sdMC extends JavaPlugin {

    private static final String currencyDIR = "plugins/RPGmod/miningChallenge/";
    private static final Logger log = Logger.getLogger("Minecraft");
    private sdMinningListener blocklistener = new sdMinningListener();
    private sdPlayerListener Playerlistener = new sdPlayerListener();
    public PermissionHandler Permissions;
    private boolean UsePermissions;

    public static void main(String[] args) {
        // TODO Debug
    }

    public void onDisable() {
        System.out.println("disabled, goodbye wold");
    }

    public void onEnable() {
        PluginDescriptionFile pdfFile = this.getDescription();
        PluginManager pm = getServer().getPluginManager();
        new File(currencyDIR).mkdirs();
        MiningProperties.loadMiningProps();
        setupPermissions();
        getCommand("mining").setExecutor(new sdCMiningCmds(this));
        pm.registerEvent(Event.Type.BLOCK_BREAK, this.blocklistener, Priority.Normal, this);
        pm.registerEvent(Event.Type.BLOCK_PLACE, this.blocklistener, Priority.Normal, this);
        pm.registerEvent(Event.Type.ENTITY_DAMAGE, this.Playerlistener, Priority.Normal, this);
        System.out.println(pdfFile.getName() + " version " + pdfFile.getVersion() + " is enabled!");
    }

    private void setupPermissions() {
        Plugin test = this.getServer().getPluginManager().getPlugin("Permissions");

        if (this.Permissions == null) {
            if (test != null) {
                UsePermissions = true;
                this.Permissions = ((Permissions) test).getHandler();
                System.out.println("Permissions is enable for sdBountyHunter.");
            } else {
                log.info("Permission system not detected, defaulting to OP");
                UsePermissions = false;
            }
        }
    }

    public boolean casUseMiningInfo(Player p) {
        if (UsePermissions) {
            return this.Permissions.has(p, "mining.info");
        }
        return p.isOp();
    }

    public boolean casUseMiningList(Player p) {
        if (UsePermissions) {
            return this.Permissions.has(p, "mining.list");
        }
        return p.isOp();
    }

    public boolean casUseMiningSet(Player p) {
        if (UsePermissions) {
            return this.Permissions.has(p, "mining.set");
        }
        return p.isOp();
    }

    public boolean casUseMiningJoin(Player p) {
        if (UsePermissions) {
            return this.Permissions.has(p, "mining.join");
        }
        return p.isOp();
    }

    public boolean casUseMiningHelp(Player p) {
        if (UsePermissions) {
            return this.Permissions.has(p, "mining.help");
        }
        return p.isOp();
    }
}
